<?php

/*
Plugin Name: SponsorPress
Plugin URI: http://www.restezconnectes.fr/plugins/sponsorpress.zip
Description: Créez des défis sportifs pour vos projets humanitaires.
Author: Florent Maillefaud
Author URI: http://www.restezconnectes.fr/
Version: 1.0.1
*/


/*
Change Log
13/06/2013 - Corrections bugs W7
08/02/2013 - Création du Plugin
*/

/// Inclusion des fichiers nécessaires
require('wpspo-includes/wpspo_constantes.php');
include("wpspo-includes/wpspo_functions.php");
include("wpspo-includes/wpspo_shortcodes.php");
include("wpspo-includes/wpspo_user_avatars.php");
include("wpspo-includes/wpspo_maps.php");
include("wpspo-includes/wpspo_metabox.php");
include("wpspo-includes/wpspo_widgets.php");
include("uninstall.php");

// Inclusion des classes
include("wpspo-includes/wpspo-classes/wpspo_object.php");
include('wpspo-includes/wpspo-classes/wpspo_defis.php');
include('wpspo-includes/wpspo-classes/wpspo_promesse.php');
include('wpspo-includes/wpspo-classes/wpspo_users.php');

// Inclure la page des crons à exécuter
//include("wpspo-includes/wpspo_crons.php");

if (!class_exists("wp_sponsorpress")) {
    
    class wp_sponsorpress {

        var $adminOptionsName = 'wp_sponsorpressAdminOptions';
        var $userOptionsName = 'wp_sponsorpressUserOptions';
        var $version = '1.1.0';
        //private $plugin_settings_tabs = array();
        
        /**
         * Constructeur
         */
        function wp_sponsorpress() {
            
        }
        
        /**
         * Initialisation
         */
        function init() {
            $this->getAdminOptions();
        }
        
        function getAdminOptions() {
         
            if(!get_option('wpspo_plugin_settings')) { 
                $wpspo_AdminOptions = array(  
                    'emaildefi' => 1,  
                    'emaildefimodif' => 0,
                    'promdefi' => 1,
                    'affmaps' => 1,
                    'limitdate' => 2,
                    'emailplugin' => get_bloginfo('admin_email'),
                    'listtypedefi' => 'Un particulier;Un groupe de jeunes;Une Colonie;Une Association'
                );  
                $get_pluginSettings = get_option('wpspo_plugin_settings');  
                if (!empty($get_pluginSettings)) {  
                    foreach ($get_pluginSettings as $key => $option) {
                        $wpspo_AdminOptions[$key] = $option;
                    }
                }  
                update_option('wpspo_plugin_settings', $wpspo_AdminOptions);  
            }
            if(!get_option( 'wpspo_currency' )) { update_option('wpspo_currency', 'EUR'); }
            if(!get_option( 'wpspo_currency_pos' )) { update_option('wpspo_currency_pos', 'right_space'); }
            if(!get_option( 'wpspo_emails_settings' )) { 
                $wpspo_EmailOptions = array(
                    'emailtexte' => 'Bonjour #_LOGIN,<br /><br />

Votre défi #_DEFI pour le projet #_PROJET a bien été inséré dans notre base.<br /><br />

Vous pouvez le voir à cette URL : <a href="#_LINK_DEFI" target="_blank">#_DEFI</a><br /><br />

Pour le modifier rendez-vous dans votre <a href="#_LINK_ESPACEPERSO" target="_blank">compte</a><br /><br />

N\'hésitez pas à nous contacter si vous avez des questions : <a href="#_LINK_CONTACT" target="_blank">Contact</a><br /><br />

',
                    'emailtextemodif' => 'Bonjour #_ADMIN,<br /><br />

Le défi <a href="#_LINK_DEFI" target="_blank">#_DEFI</a> vient d\'être modifié.<br /><br />
',
                    
                    'email_txt_newpromesse' => 'Bonjour #_LOGIN,<br /><br />
    
Une promesse de #_TOTAL_PROMESSSE a été faite pour vous sponsoriser.<br /><br />

Elle concerne ce défi <a href="#_LINK_DEFI" target="_blank">#_DEFI</a>.<br /><br />

',
                     'email_txt_thankspromesse' => 'Bonjour #_LOGIN,<br /><br />
    
Merci pour votre promesse de #_TOTAL_PROMESSSE pour sponsoriser #_PARTICIPANT.<br /><br />

Elle concerne le défi <a href="#_LINK_DEFI" target="_blank">#_DEFI</a>.<br /><br />

Nous vous remercions pour votre participation.<br /><br />
',
                    
                    'email_txt_participate' => 'Bonjour #_LOGIN,
                    
#_PARTICIPANT a décidé de participer au défi <a href="#_LINK_DEFI" target="_blank">#_DEFI</a>.<br /><br />
',
                    'email_txt_challenge' => 'Bonjour #_LOGIN,<br /><br />
                    
#_PARTICIPANT vient de vous parrainner pour le défi <a href="#_LINK_DEFI" target="_blank">#_DEFI</a>.<br /><br />
',
                    'email_txt_challenge_confirm' => 'Bonjour #_LOGIN,<br /><br />
                    
Votre parrainnage pour le défi <a href="#_LINK_DEFI" target="_blank">#_DEFI</a> pour un montant de #_PROMESSE a bien été enregistré.<br /><br />

',
                    'email_txt_challenge_delete' => 'Bonjour #_LOGIN,<br /><br />
                    
#_PARTICIPANT a décidé de supprimer sa promesse pour le défi <a href="#_LINK_DEFI" target="_blank">#_DEFI</a>.<br /><br />

',
                    
                    'email_txt_participant' => 'Bonjour #_LOGIN,<br /><br />
                    
#_PARTICIPANT a décidé de vous sponsoriser pour votre participation au défi <a href="#_LINK_DEFI" target="_blank">#_DEFI</a>.<br /><br />

',
                    'email_txt_participant_confirm' => 'Bonjour #_LOGIN,<br /><br />
                    
Votre parrainnage sur #_PARTICIPANT pour un montant de #_PROMESSE a bien été enregistré.<br /><br />
',
                  'email_txt_invite' => '

Bonjour,<br /><br />

#_LOGIN vous invite à participer à son défi <b>#_LINK_DEFI</b><br /><br />

Si vous n\'avez pas de compte, vous pouvez un <a href="/wp-login.php?action=register" target="_blank">créer un compte maintenant</a> ou <a href="/wp-login.php" target="_blank">identifiez-vous</a>.<br /><br />

',
                    'email_txt_challenge_close' => '
Bonjour #_PARTICIPANT,<br /><br />

Le défi <a href="#_LINK_DEFI" target="_blank"><b>#_DEFI</b></a> est terminé<br /><br />

Rendez-vous maintenant sur <a href="#_LINK_ESPACEPERSO" target="_blank">votre compte</a> afin de cloturer votre particiption.<br /><br />

',
                    
                    'email_txt_finish_challenge' => '
Bonjour #_LOGIN,<br /><br />

#_PARTICIPANT a finalisé sa participation au défi <a href="#_LINK_DEFI" target="_blank"><b>#_DEFI</b></a><br /><br />

Rendez-vous maintenant sur <a href="#_LINK_ESPACEPERSO" target="_blank">votre compte</a> afin de finaliser votre promesse.<br /><br />
',
                    
                    'email_txt_signature' => '
Le staff de #_SITE<br /><a href="#_LINK_DEFI" target="_blank">#_LINK_SITE</a><br /><br />
<a href="#_LINK_ESPACEPERSO" target="_blank">Mon compte</a> | <a href="#_LINK_CONTACT" target="_blank">Contact</a><br /><br /><br />Note : Cet e-mail est automatique, merci de ne pas répondre.

',
                    'email_txt_bottom' => '
<a href="#_LINK_ESPACEPERSO" target="_blank">Mon compte</a> | <a href="#_LINK_CONTACT" target="_blank">Contact</a><br /><br />© 2015 - <a href="#_LINK_DEFI" target="_blank">#_LINK_SITE</a>
'
                    
                    
                );
                $get_emailSettings = get_option('wpspo_emails_settings');  
                if (!empty($get_emailSettings)) {  
                    foreach ($get_emailSettings as $key => $option) {
                        $wpspo_EmailOptions[$key] = $option;
                    }
                }  
                update_option('wpspo_emails_settings', $wpspo_EmailOptions);
            }

        }


         /**
         * Installation du plugin wp_sponsorpress
         * @global wpdb $wpdb
         */
        function wp_sponsorpress_install() {
            
            global $wpdb;
            
            load_plugin_textdomain('sponsorpress', false, dirname( plugin_basename( __FILE__ ) ).'/wpspo-lang');
            $upSeeting = $this->init();
            $sql = '';
            
            /* Création des tables nécessaires */
            if($wpdb->get_var("SHOW TABLES LIKE '".WSPO_PROMESSE."'") != WSPO_PROMESSE) {

                $sql .= "CREATE TABLE `".WSPO_PROMESSE."` (
                    `promesse_id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
                    `promesse_status` tinyint(2) NOT NULL DEFAULT '1',
                    `promesse_id_defi` bigint(20) unsigned NOT NULL,
                    `promesse_id_projet` bigint(20) unsigned NOT NULL,
                    `promesse_texte` text NOT NULL,
                    `promesse_id_user` bigint(20) unsigned NOT NULL,
                    `promesse_id_parent` bigint(20) unsigned NOT NULL,
                    `promesse_montant` decimal(10,2) unsigned NOT NULL DEFAULT '0.00',
                    `promesse_total` decimal(10,2) unsigned NOT NULL DEFAULT '0.00',
                    `promesse_valide` tinyint(2) NOT NULL DEFAULT '0',
                    PRIMARY KEY (`promesse_id`)
                   ) ;";
            }
            if($wpdb->get_var("SHOW TABLES LIKE '".WSPO_DEFIS."'") != WSPO_DEFIS) {
                $sql .= "CREATE TABLE IF NOT EXISTS `".WSPO_DEFIS."` (
                      `defis_id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
                      `defis_id_actif` tinyint(2) unsigned NOT NULL DEFAULT '1',
                      `defis_id_defi` bigint(20) unsigned NOT NULL DEFAULT '0',
                      `defis_id_projet` bigint(20) unsigned NOT NULL DEFAULT '0',
                      `defis_id_user` bigint(20) unsigned NOT NULL DEFAULT '0',
                      `defis_option_value` longtext NOT NULL,
                      `defis_total` varchar(20) NOT NULL DEFAULT '0',
                      PRIMARY KEY (`defis_id`)
                    ) ;";
                require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
                dbDelta($sql);
            }
            if($wpdb->get_var("SHOW TABLES LIKE '".WSPO_USERS."'") != WSPO_USER) {
                $sql .= "CREATE TABLE IF NOT EXISTS `".WSPO_USERS."` (
                      `users_id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
                      `users_id_user` bigint(20) unsigned NOT NULL DEFAULT '0',
                      `users_id_statut` tinyint(2) unsigned NOT NULL DEFAULT '1',
                      `users_id_defi` bigint(20) unsigned NOT NULL DEFAULT '0',
                      `users_realise` decimal(8,2) unsigned NOT NULL DEFAULT '0.00',
                      `users_newletter` INT(2) NOT NULL DEFAULT '0',
                      `users_option_value` longtext NOT NULL,
                      PRIMARY KEY (`users_id`)
                    ) ;";
                require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
                dbDelta($sql);
            }
            
            /* Création des nouveaux rôles */
            $wpspo_role = array('Participant'=>'participant');
            
            foreach($wpspo_role as $wpspo_roleusertitle=>$wpspo_roleuser) {
                $subRole = get_role( 'subscriber' );
                add_role($wpspo_roleuser, $wpspo_roleusertitle, $subRole->capabilities);
            }

            /* Création des pages automatiques nécessaires */
            $wpspo_pages_id = get_option('wp_sponsorpress_projets'); // Les id des pages sont en mémoire
            if( empty($wpspo_pages_id) ) { // Test si elles existent pour ne pas les recréer
                
                $post_data = array(
                    'post_status' => 'publish',
                    'post_type' => 'page',
                    'post_content' => 'NE RIEN ECRIRE ICI : [wpspo-listprojets]',
                    'post_excerpt' => '',
                    'comment_status' => 'closed',
                    'ping_status'    => 'closed',
                    'post_title' => __('Projects supported', 'sponsorpress'),
                    'menu_order' => 0
                );
                $wpspo_post_projet_id = wp_insert_post($post_data, false);
                //update_post_meta($wpspo_post_projet_id, "_wp_page_template", WPSPO_THEME_URL."wpspo-profil.php");
                update_option('wp_sponsorpress_projets', $wpspo_post_projet_id);

                if( $wpspo_post_projet_id > 0 ) {

                    $post_data_defis = array(
                        'post_status' => 'publish',
                        /*'post_parent' => $wpspo_post_projet_id,*/
                        'post_type' => 'page',
                        'ping_status' => get_option('default_ping_status'),
                        'post_content' => 'NE RIEN ECRIRE ICI : [wpspo-listdefis]',
                        'post_excerpt' => '',
                        'comment_status' => 'closed',
                        'ping_status'    => 'closed',
                        'post_title' => __('Challenges', 'sponsorpress'),
                        'menu_order' => 1
                    );
                    $wpspo_id_defis = wp_insert_post($post_data_defis, false);
                    update_option('wp_sponsorpress_defis', $wpspo_id_defis);
                    
                    $post_data_gestion = array(
                        'post_status' => 'publish',
                        'post_parent' => '',
                        'post_type' => 'page',
                        'ping_status' => get_option('default_ping_status'),
                        'post_content' => 'NE RIEN ECRIRE ICI : [wpspo-magestion]',
                        'post_excerpt' => '',
                        'comment_status' => 'closed',
                        'ping_status'    => 'closed',
                        'post_title' => __('My account', 'sponsorpress'),
                        'menu_order' => 5
                    );
                    $wpspo_id_gestion = wp_insert_post($post_data_gestion, false);
                    update_option('wp_sponsorpress_gestion', $wpspo_id_gestion);
                    
                    $post_data_organiser = array(
                        'post_status' => 'publish',
                        'post_parent' => '',
                        'post_type' => 'page',
                        'ping_status' => get_option('default_ping_status'),
                        'post_content' => __('<strong>You are responsible for an association, a camp, a group of young...</strong><br />Join us and together realize a better world!<br /><br />Come and create a challenge suitable for young you are responsible and give them the opportunity to take concrete action against poverty.<br /><br />With a few clicks, find on our website all the elements you need to organize and run your event.<br /><br />Download the PDF guide for organizers', 'sponsorpress'),
                        'post_excerpt' => '',
                        'comment_status' => 'closed',
                        'ping_status'    => 'closed',
                        'post_title' => __('Organize a challenge', 'sponsorpress'),
                        'menu_order' => 3
                    );
                    $wpspo_id_organiser = wp_insert_post($post_data_organiser, false);
                    update_option('wp_sponsorpress_organiser', $wpspo_id_organiser);
                    
                    $post_data_archives = array(
                        'post_status' => 'publish',
                        'post_parent' => $wpspo_post_projet_id,
                        'post_type' => 'page',
                        'ping_status' => get_option('default_ping_status'),
                        'post_content' => 'NE RIEN ECRIRE ICI',
                        'post_excerpt' => '',
                        'comment_status' => 'closed',
                        'ping_status'    => 'closed',
                        'post_title' => __('Archives', 'sponsorpress'),
                        'menu_order' => 1
                    );
                    $wpspo_id_archives = wp_insert_post($post_data_archives, false);
                    update_option('wp_sponsorpress_archives', $wpspo_id_archives);
                    
                    $post_data_participants = array(
                        'post_status' => 'publish',
                        'post_parent' => $wpspo_id_defis,
                        'post_type' => 'page',
                        'ping_status' => get_option('default_ping_status'),
                        'post_content' => 'NE RIEN ECRIRE ICI',
                        'post_excerpt' => '',
                        'comment_status' => 'closed',
                        'ping_status'    => 'closed',
                        'post_title' => __('Participants', 'sponsorpress'),
                        'menu_order' => 1
                    );
                    $wpspo_id_participants = wp_insert_post($post_data_participants, false);
                    update_option('wp_sponsorpress_participants', $wpspo_id_participants);
                    
                }
            }
            $option['wp_sponsorpress_version'] = $this->version;
            add_option('wp_sponsorpress_version', $option);
            
        }
        
        
        
    }
}

if (class_exists("wp_sponsorpress")) {
    
    $inst_wp_sponsorpress = new wp_sponsorpress();
    
    if(function_exists('register_activation_hook')) {
        register_activation_hook(__FILE__, array(&$inst_wp_sponsorpress, 'wp_sponsorpress_install'));
    }
    if(function_exists('register_deactivation_hook')) {
        register_deactivation_hook(__FILE__, 'wp_sponsorpress_uninstall');
    }
    // multilingue
    load_plugin_textdomain('sponsorpress', false, dirname( plugin_basename( __FILE__ ) ).'/wpspo-lang');

    // Ajoute les liens utiles dans la page des plugins
    function wpspo_settings_action_links( $links, $file ) {
    
        // lien vers la page d'accueil de ce plugin
        array_unshift( $links, '<a href="' . admin_url( 'admin.php?page=sponsor-dashboard' ) . '">' . __( 'Dashboard', 'sponsorpress' ) . '</a>' );
        // lien vers la page de config de ce plugin
        array_push( $links, '<a href="' . admin_url( 'admin.php?page=sponsor-settings' ) . '">' . __( 'Settings', 'sponsorpress' ) . '</a>' );

        return $links;
    }
    add_filter( 'plugin_action_links_'.plugin_basename( __FILE__ ), 'wpspo_settings_action_links', 10, 2 );

    /* Créé un menu dans l'admin*/
    function wpspo_options_panel(){
        add_menu_page( 'Theme page title', 'SponsorPress', 'publish_posts', 'wpspo_admin_wpspomenu', 'wpspo_dashboard_page', plugins_url( ''.WPSPO_NAME_DIR.'/wpspo-images/sponsor-menu-icon.png' ), 10 );
        add_submenu_page( 'wpspo_admin_wpspomenu', __('Promises management','sponsorpress'), __('Promises management','sponsorpress'), 'manage_options', 'sponsor-dashboard', 'wpspo_dashboard_page');
        add_submenu_page( 'wpspo_admin_wpspomenu', __('Settings','sponsorpress'), __('Settings','sponsorpress'), 'manage_options', 'sponsor-settings', 'wpspo_settings_page');
        add_submenu_page( 'wpspo_admin_wpspomenu', __('About','sponsorpress'), __('About','sponsorpress'), 'manage_options', 'sponsor-about', 'wpspo_about_page');
        
    }
    add_action('admin_menu', 'wpspo_options_panel');

    
    /**************************************************/
    /*************** LES PROJETS *********************/
    /************************************************/
    if ( ! function_exists('custom_wpspo_projets') ) {

        // Register Custom Post Type --> Projets
        function custom_wpspo_projets() {
            $labels = array(
                'name'                => __( 'Project', 'sponsorpress' ),
                'singular_name'       => __( 'Project', 'sponsorpress' ),
                'menu_name'           => __( 'Project', 'sponsorpress' ),
                'parent_item_colon'   => __( 'Project Parent:', 'sponsorpress' ),
                'all_items'           => __( 'Projects Management', 'sponsorpress' ),
                'view_item'           => __( 'View Project', 'sponsorpress' ),
                'add_new_item'        => __( 'Add a Project', 'sponsorpress' ),
                'add_new'             => __( 'New Project', 'sponsorpress' ),
                'edit_item'           => __( 'Edit Project', 'sponsorpress' ),
                'update_item'         => __( 'Update Project', 'sponsorpress' ),
                'search_items'        => __( 'Find project', 'sponsorpress' ),
                'not_found'           => __( 'Project not found', 'sponsorpress' ),
                'not_found_in_trash'  => __( 'Project not found in the trash', 'sponsorpress' ),
            );

            $args = array(
                'label'               => __( 'project', 'sponsorpress' ),
                'description'         => __( 'Information of projects page', 'sponsorpress' ),
                'labels'              => $labels,
                'taxonomies'          => array( 'category', 'post_tag' ),
                'hierarchical'        => false,
                'public'              => true,
                'show_ui'             => true,
                'show_in_menu'        => 'wpspo_admin_wpspomenu',
                'show_in_nav_menus'   => true,
                'show_in_admin_bar'   => false,
                'can_export'          => true,
                'map_meta_cap'        => true,
                'has_archive'         => true,
                'query_var'           => true,
                'rewrite'             => array( 'slug' => 'projet' ),
                'exclude_from_search' => false,
                'publicly_queryable'  => true,
                'capability_type'     => 'post',
                '_edit_link'          => 'post.php?post=%d',
                'supports' => array('title','editor','thumbnail','custom-fields', 'author', 'comments'),
            );

            register_post_type( 'projet', $args );
            register_taxonomy_for_object_type('post_tag', 'projet','show_tagcloud=1&hierarchical=true'); 
            
            if ( !get_role('sponsorpress') ) {
                global $wp_roles;
                if ( !isset( $wp_roles ) ) $wp_roles = new WP_Roles();

                $caps = $wp_roles->get_role('editor')->capabilities; // <------ on lui attribu les capacité du role éditeur
                $caps = array_merge( $caps, array( 'edit_projet' => true, 'publish_projet' => true, 'edit_defi' => true, 'publish_defi' => true, 'edit_page' => false, 'publish_page' => false ) ); // <------ on autorise les capacité du role SponsorPress
                $wp_roles->add_role( 'sponsorpress', 'Gestion SponsorPress', $caps );
            }
        }

        // Hook into the 'init' action
        add_action( 'init', 'custom_wpspo_projets', 0 );

    }
    
    // Register Custom Taxonomy --> Projets
    /*function custom_taxonomy_cat_projets()  {
        $labels = array(
            'name'                       => __( 'Projects Categories', 'sponsorpress' ),
            'singular_name'              => __( 'Project category', 'sponsorpress' ),
            'menu_name'                  => __( 'Projects Categories', 'sponsorpress' ),
            'all_items'                  => __( 'All', 'sponsorpress' ),
            'parent_item'                => __( 'Parent category', 'sponsorpress' ),
            'parent_item_colon'          => __( 'Parent category:', 'sponsorpress' ),
            'new_item_name'              => __( 'New category', 'sponsorpress' ),
            'add_new_item'               => __( 'Add a category', 'sponsorpress' ),
            'edit_item'                  => __( 'Edit a category', 'sponsorpress' ),
            'update_item'                => __( 'Update Category', 'sponsorpress' ),
            'separate_items_with_commas' => __( 'Separate categories with commas', 'sponsorpress' ),
            'search_items'               => __( 'Find a category', 'sponsorpress' ),
            'add_or_remove_items'        => __( 'Add or delete a catégory', 'sponsorpress' ),
            'choose_from_most_used'      => __( 'Choose from the most used categories', 'sponsorpress' ),
        );

        $args = array(
            'labels'                     => $labels,
            'hierarchical'               => true,
            'public'                     => true,
            'show_ui'                    => true,
            'show_admin_column'          => true,
            'show_in_nav_menus'          => true,
            'show_tagcloud'              => true,
        );

        register_taxonomy( 'wpspo-cat-projets', 'projet', $args );
    }

    // Hook into the 'init' action
    add_action( 'init', 'custom_taxonomy_cat_projets', 0 );*/
    
    
    /**************************************************/
    /********************* LES DEFIS *****************/
    /************************************************/
    
    if ( ! function_exists('custom_wspo_defis') ) {

        // Register Custom Post Type --> défis
        function custom_wspo_defis() {
            $labels = array(
                'name'                => __( 'Challenge', 'sponsorpress' ),
                'singular_name'       => __( 'Challenge', 'sponsorpress' ),
                'menu_name'           => __( 'Challenge', 'sponsorpress' ),
                'parent_item_colon'   => __( 'Challenge parent :', 'sponsorpress' ),
                'all_items'           => __( 'Challenge management', 'sponsorpress' ),
                'view_item'           => __( 'View Challenge', 'sponsorpress' ),
                'add_new_item'        => __( 'Add a new Challenge', 'sponsorpress' ),
                'add_new'             => __( 'New Challenge', 'sponsorpress' ),
                'edit_item'           => __( 'Edit a Challenge', 'sponsorpress' ),
                'update_item'         => __( 'Update a Challenge', 'sponsorpress' ),
                'search_items'        => __( 'Find a Challenge', 'sponsorpress' ),
                'not_found'           => __( 'No challenge found', 'sponsorpress' ),
                'not_found_in_trash'  => __( 'No challenge found in the trash', 'sponsorpress' ),
            );

            $args = array(
                'label'               => __( 'challenge', 'sponsorpress' ),
                'singular_label'      => __( 'Challenge', 'sponsorpress' ),
                'description'         => __( 'Information of challenges page', 'sponsorpress' ),
                'labels'              => $labels,
                'taxonomies'          => array( 'category', 'post_tag' ),
                'hierarchical'        => false,
                'public'              => true,
                'show_ui'             => true,
                'show_in_menu'        => 'wpspo_admin_wpspomenu',
                'show_in_nav_menus'   => true,
                'show_in_admin_bar'   => false,
                'can_export'          => true,
                'map_meta_cap'        => true,
                'has_archive'         => true,
                'query_var'           => true,
                'rewrite'             => array( 'slug' => 'defi' ),
                'exclude_from_search' => false,
                'publicly_queryable'  => true,
                'capability_type'     => 'post',
                '_edit_link'          => 'post.php?post=%d',
                'supports' => array('title','editor','thumbnail','custom-fields', 'author', 'comments'),
            );

            register_post_type( 'defi', $args );
            register_taxonomy_for_object_type('post_tag', 'defi','show_tagcloud=1&hierarchical=true'); 
        }

        // Hook into the 'init' action
        add_action( 'init', 'custom_wspo_defis', 0 );

    }
    
    // Register Custom Taxonomy --> Défis
    /*function custom_taxonomy_cat_defis()  {
        $labels = array(
            'name'                       => _x( 'Categories of challenges', 'Taxonomy General Name', 'sponsorpress' ),
            'singular_name'              => _x( 'Category of challenge', 'Taxonomy Singular Name', 'sponsorpress' ),
            'menu_name'                  => __( 'Categories of challenges', 'sponsorpress' ),
            'all_items'                  => __( 'All', 'sponsorpress' ),
            'parent_item'                => __( 'Parent category', 'sponsorpress' ),
            'parent_item_colon'          => __( 'Parent category:', 'sponsorpress' ),
            'new_item_name'              => __( 'New category', 'sponsorpress' ),
            'add_new_item'               => __( 'Add a category', 'sponsorpress' ),
            'edit_item'                  => __( 'Edit a category', 'sponsorpress' ),
            'update_item'                => __( 'Update Category', 'sponsorpress' ),
            'separate_items_with_commas' => __( 'Separate categories with commas', 'sponsorpress' ),
            'search_items'               => __( 'Find a category', 'sponsorpress' ),
            'add_or_remove_items'        => __( 'Add or delete a category', 'sponsorpress' ),
            'choose_from_most_used'      => __( 'Choose from the most used categories', 'sponsorpress' ),
        );

        $args = array(
            'labels'                     => $labels,
            'hierarchical'               => true,
            'public'                     => true,
            'show_ui'                    => true,
            'show_admin_column'          => true,
            'show_in_nav_menus'          => true,
            'show_tagcloud'              => true,
        );

        register_taxonomy( 'wpspo-cat-defis', 'defis', $args );
    }

    // Hook into the 'init' action
    add_action( 'init', 'custom_taxonomy_cat_defis', 0 );
    */
       
    function wpspo_settings_page() {
            //must check that the user has the required capability
        if (!current_user_can('manage_options')) {
          wp_die( __("You do not have sufficient privileges to access this page.", 'sponsorpress') );
        }
        include WPSPO_THEME_ADMIN."settings.php";

    }
    
    function wpspo_about_page() {
            //must check that the user has the required capability
        if (!current_user_can('manage_options')) {
          wp_die( __("You do not have sufficient privileges to access this page.", 'sponsorpress') );
        }
        include WPSPO_THEME_ADMIN."about.php";
    }

    function wpspo_dashboard_page() {

        //must check that the user has the required capability
        if (!current_user_can('manage_options')) {
          wp_die( __("You do not have sufficient privileges to access this page.", 'sponsorpress') );
        }
        include WPSPO_THEME_ADMIN."dashboard.php";
    }


    
    /*
     * Ajout un formualaire sur la page organiser
     */
    /*function add_btn_content($content) {

        //global $single;
        global $post;
        if( is_page(WPSPO_ID_ORGANISER) ) {
            if($_POST["post_accept"]==true && $_POST['accepte_cond'] == '') {
                $condError = __('Please accept the conditions to organize a challenge!', 'sponsorpress');
                $addContent .= '<span class="error">'.$condError.'</span><br /><br />';
            }
            $addContent .= '<a name="valide"></a><form method="post" name="add_participant" action="#valide">';
            
            $addContent .= '<input type="checkbox" id="accepte_cond" name="accepte_cond" value="1" /> '.__('I have read the guide for organizers and I agree', 'sponsorpress').'';
            
            $addContent .= '<INPUT type="hidden" name="post_accept" value="true">';
            $addContent .= '</form>';
            $addContent .= '[wpspobutton link="" onclick="window.document.add_participant.submit();" content="'.__('Start Adventure', 'sponsorpress').'"]';
        }
        return $content.$addContent;
    }*/
    //add_filter('the_content', 'add_btn_content', 10);
    

    
     /*
     * On va chercher les différents paramètres (venant de l'admin)
     */
    if(get_option('wpspo_plugin_settings')) { extract(get_option('wpspo_plugin_settings')); }
    $paramWpspoSetting = get_option('wpspo_plugin_settings');
    // et pour les emails
    if(get_option('wpspo_emails_settings')) { extract(get_option('wpspo_emails_settings')); }
    $paramWpspoEmailSetting = get_option('wpspo_emails_settings');

    // Intègre la feuille de style 
    function wpspo_css() {
        $url = WP_PLUGIN_URL.'/'.WPSPO_NAME_DIR.'/wpspo-css/wpspo_styles.css';
        echo "<link href='$url' rel='stylesheet' id='sponsor-css' type='text/css' media='screen' />";
    }
    add_action('wp_head', 'wpspo_css' );
    
/*
    function wpspo_showMessagesInAdmin() {

        $pluginsActive = get_option('active_plugins');
        if ( !in_array( 'easy-fancybox/easy-fancybox.php' , $pluginsActive ) )  {
            $NoFancy = true;
        }
        //on cible uniquement les administrateurs  (pas les éditeurs ou contributeurs)
        if (current_user_can('manage_options') && $NoFancy==true) {
           echo '<div class="error"><p><strong>SponsorPress</strong> utilise le plugin <a href=\"http://wordpress.org/extend/plugins/easy-fancybox/\" target=\"_blank\">Easy FancyBox</a>. Merci de le télécharger puis de l\'installer et l\'activer. <a href="http://wordpress.org/extend/plugins/easy-fancybox/" target="_blank">Téléchargez-le ici</a></p>
            </div>';
        }
        
    }
    add_action('admin_notices', 'wpspo_showMessagesInAdmin');
 */

    
}


